package jp.sourceforge.akjrcp.draw;

import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import jp.sourceforge.akjrcp.text_douga.DougaPreference;
import jp.sourceforge.akjrcp.text_douga.item.TextDougaTextUtils;

public class TextLinesLayout {
private String[] texts;
private TextLayout[] textLayouts;
//private String baseText;
private Font font;
private int lineMargin=6;//between text and text

private Shape[] shapes;
public Font getFont() {
	return font;
}
public void setFont(Font font) {
	this.font = font;
}

public TextLinesLayout(Graphics2D g2,Font font,String text,int maxWidth){
	this(g2,font,text,maxWidth,maxWidth);
}
public TextLinesLayout(Graphics2D g2,Font font,String text,int firstWidth,int maxWidth){
	//this.baseText=text;
	this.font=font;
	texts=wrapText(font, text, firstWidth,maxWidth);
	
	for (int i = 0; i < texts.length; i++) {
		if(texts[i].equals("")){//empty string is for textlayout
			texts[i]=" ";//so change space only
		}
	}
	
	textLayouts=new TextLayout[texts.length];
	
	for (int i = 0; i < textLayouts.length; i++) {
		textLayouts[i]=makeStringLaytou(getTextAt(i), font, g2);
	}
}
/*
public void dispose(){
	if(textLayouts!=null){
		for (int i = 0; i < textLayouts.length; i++) {
			textLayouts[i]=null;
		}
	}
}*/

public String getTextAt(int index){
	return texts[index];
}
public TextLayout getTextLayoutAt(int index){
	return textLayouts[index];
}


public  TextLayout makeStringLaytou(String text,Font font,Graphics2D g2){
	TextLayout textLayout =
	  new TextLayout(text, font, g2.getFontRenderContext());
	//Shape shape = textLayout.getOutline(new AffineTransform());
	//g2.dispose();//wao this is the BUG as 1 frame never written!
	return textLayout;
	}

public String[] wrapText(Font font,String text,int firstWidth,int maxwidth){
	return TextDougaTextUtils.wrapText(text, font, firstWidth,maxwidth);
}
/*
public String getBaseText() {
	return baseText;
}
public void setBaseText(String baseText) {
	this.baseText = baseText;
}*/

public Shape[] getShapes(){
	if(shapes==null){
		shapes=new Shape[textLayouts.length];
		for (int i = 0; i < shapes.length; i++) {
		shapes[i]=textLayouts[i].getOutline(new AffineTransform());
		}
	}
	return shapes;
}

public Rectangle getBounds(){
	Rectangle rect=new Rectangle();
	rect.height=textLayouts.length*font.getSize()+(textLayouts.length-1)*lineMargin;
	
	for (int i = 0; i < textLayouts.length; i++) {
		
		//rect.width=Math.max(rect.width, textLayouts[i].getOutline(new AffineTransform()).getBounds().width); //out line is small 
		//System.out.println("bounds:"+textLayouts[i].getBounds());
		rect.width=(int) Math.max(rect.width, textLayouts[i].getBounds().getWidth()+textLayouts[i].getBounds().getX());
	}
	return rect;
}
public TextLayout[] getTextLayouts() {
	return textLayouts;
}
public String[] getTexts() {
	return texts;
}

}
